import 'package:flutter/material.dart';
import 'package:youfangtong_module/channel_maintainer/message/page/message_page.dart';
import 'package:youfangtong_module/channel_maintainer/my/page/mine_page.dart';
import 'package:youfangtong_module/channel_maintainer/workbench/page/workbench_page.dart';
import 'package:youfangtong_module/net/dio_utils.dart';
import 'package:youfangtong_module/net/http_api.dart';
import 'package:youfangtong_module/widgets/my_colors.dart';

/// 主页
class ChannelMaintainerTabBar extends StatefulWidget {

  const ChannelMaintainerTabBar({super.key});

  @override
  State<StatefulWidget> createState() {
    return _ChannelMaintainerTabBarState();
  }

}

class _ChannelMaintainerTabBarState extends State<ChannelMaintainerTabBar> {

  final WorkBenchPage _workBenchPage = const WorkBenchPage();

  final MessagePage _messagePage = const MessagePage();

  final MinePage _minePage = const MinePage();

  // 当前选中Bottom的索引
  int _selectedIndex = 0;

  /// 进行些初始化
  @override
  void initState() {
    super.initState();
    // 布局绘制完成后的回调
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      refreshMessageUnRead();
    });
  }

  @override
  Widget build(BuildContext context) {

    // 页面的数组
    final List<Widget> pages = [_workBenchPage,_messagePage,_minePage];

    // 页面控制器
    final PageController controller = PageController();

    return Scaffold(
      body: PageView(
        controller: controller,
        physics: const NeverScrollableScrollPhysics(),
        children: pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: _getItems(),
        currentIndex: _selectedIndex,
        selectedFontSize: 12,
        unselectedFontSize: 12,
        selectedItemColor: MyColors.color_222222,
        unselectedItemColor: MyColors.color_B2B2B2,
        onTap: (index) {
          setState(() {
            _selectedIndex = index;
            controller.jumpToPage(index); // 切换PageView
          });
        },
      ),
    );
  }

  _getItems() {
    List<BottomNavigationBarItem> items = [];
    items.add(_bottomItem(Image.asset("assets/images/icon_tabbar_home.png"), Image.asset("assets/images/icon_tabbar_home_sel.png"), "工作台"));
    items.add(_bottomItem(Image.asset("assets/images/icon_tabbar_message.png"), Image.asset("assets/images/icon_tabbar_message_sel.png"), "消息"));
    items.add(_bottomItem(Image.asset("assets/images/icon_tabbar_my.png"), Image.asset("assets/images/icon_tabbar_my_sel.png"), "我的"));
    return items;
  }

  _bottomItem(Widget icon,Widget activeIcon,String label) {
    return BottomNavigationBarItem(
        icon: icon,
        activeIcon: activeIcon,
        label: label
    );
  }

  /// 刷新未读消息的数量
  refreshMessageUnRead() {
    DioUtils.getInstance()?.asyncRequestNetwork<Map>(
        context,
        Method.post,
        HttpApi.pmsUnReadMsgCount,
        onSuccess: (data) {

        }
    );
  }

}